home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / Make / source / expand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-20  |  11.5 KB  |  458 lines

  1. /* Variable expansion functions for GNU Make.
  2. Copyright (C) 1988, 89, 91, 92, 93, 95 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "filedef.h"
  21. #include "job.h"
  22. #include "commands.h"
  23. #include "variable.h"
  24.  
  25. /* The next two describe the variable output buffer.
  26.    This buffer is used to hold the variable-expansion of a line of the
  27.    makefile.  It is made bigger with realloc whenever it is too small.
  28.    variable_buffer_length is the size currently allocated.
  29.    variable_buffer is the address of the buffer.  */
  30.  
  31. static unsigned int variable_buffer_length;
  32. static char *variable_buffer;
  33.  
  34. /* Subroutine of variable_expand and friends:
  35.    The text to add is LENGTH chars starting at STRING to the variable_buffer.
  36.    The text is added to the buffer at PTR, and the updated pointer into
  37.    the buffer is returned as the value.  Thus, the value returned by
  38.    each call to variable_buffer_output should be the first argument to
  39.    the following call.  */
  40.  
  41. char *
  42. variable_buffer_output (ptr, string, length)
  43.      char *ptr, *string;
  44.      unsigned int length;
  45. {
  46.   register unsigned int newlen = length + (ptr - variable_buffer);
  47.  
  48.   if (newlen > variable_buffer_length)
  49.     {
  50.       unsigned int offset = ptr - variable_buffer;
  51.       variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
  52.                 ? newlen + 100
  53.                 : 2 * variable_buffer_length);
  54.       variable_buffer = (char *) xrealloc (variable_buffer,
  55.                        variable_buffer_length);
  56.       ptr = variable_buffer + offset;
  57.     }
  58.  
  59.   bcopy (string, ptr, length);
  60.   return ptr + length;
  61. }
  62.  
  63. /* Return a pointer to the beginning of the variable buffer.  */
  64.  
  65. static char *
  66. initialize_variable_output ()
  67. {
  68.   /* If we don't have a variable output buffer yet, get one.  */
  69.  
  70.   if (variable_buffer == 0)
  71.     {
  72.       variable_buffer_length = 200;
  73.       variable_buffer = (char *) xmalloc (variable_buffer_length);
  74.       variable_buffer[0] = '\0';
  75.     }
  76.  
  77.   return variable_buffer;
  78. }
  79.  
  80. /* Recursively expand V.  The returned string is malloc'd.  */
  81.  
  82. char *
  83. recursively_expand (v)
  84.      register struct variable *v;
  85. {
  86.   char *value;
  87.  
  88.   if (v->expanding)
  89.     {
  90.       /* Expanding V causes infinite recursion.  Lose.  */
  91.       if (reading_filename == 0)
  92.     fatal ("Recursive variable `%s' references itself (eventually)",
  93.            v->name);
  94.       else
  95.     makefile_fatal
  96.       (reading_filename, *reading_lineno_ptr, 
  97.        "Recursive variable `%s' references itself (eventually)",
  98.        v->name);
  99.     }
  100.  
  101.   v->expanding = 1;
  102.   value = allocated_variable_expand (v->value);
  103.   v->expanding = 0;
  104.  
  105.   return value;
  106. }
  107.  
  108. /* Warn that NAME is an undefined variable.  */
  109.  
  110. #ifdef __GNUC__
  111. __inline
  112. #endif
  113. static void
  114. warn_undefined (name, length)
  115.      char *name;
  116.      unsigned int length;
  117. {
  118.   if (warn_undefined_variables_flag)
  119.     {
  120.       static const char warnmsg[] = "warning: undefined variable `%.*s'";
  121.       if (reading_filename != 0)
  122.     makefile_error (reading_filename, *reading_lineno_ptr,
  123.             warnmsg, length, name);
  124.       else
  125.     error (warnmsg, length, name);
  126.     }
  127. }
  128.  
  129. /* Expand a simple reference to variable NAME, which is LENGTH chars long.  */
  130.  
  131. #ifdef __GNUC__
  132. __inline
  133. #endif
  134. static char *
  135. reference_variable (o, name, length)
  136.      char *o;
  137.      char *name;
  138.      unsigned int length;
  139. {
  140.   register struct variable *v = lookup_variable (name, length);
  141.  
  142.   if (v == 0)
  143.     warn_undefined (name, length);
  144.  
  145.   if (v != 0 && *v->value != '\0')
  146.     {
  147.       char *value = (v->recursive ? recursively_expand (v) : v->value);
  148.       o = variable_buffer_output (o, value, strlen (value));
  149.       if (v->recursive)
  150.     free (value);
  151.     }
  152.  
  153.   return o;
  154. }
  155.  
  156. /* Scan LINE for variable references and expansion-function calls.
  157.    Build in `variable_buffer' the result of expanding the references and calls.
  158.    Return the address of the resulting string, which is null-terminated
  159.    and is valid only until the next time this function is called.  */
  160.  
  161. char *
  162. variable_expand (line)
  163.      register char *line;
  164. {
  165.   register struct variable *v;
  166.   register char *p, *o, *p1;
  167.  
  168.   p = line;
  169.   o = initialize_variable_output ();
  170.  
  171.   while (1)
  172.     {
  173.       /* Copy all following uninteresting chars all at once to the
  174.          variable output buffer, and skip them.  Uninteresting chars end
  175.      at the next $ or the end of the input.  */
  176.  
  177.       p1 = index (p, '$');
  178.  
  179.       o = variable_buffer_output (o, p, p1 != 0 ? p1 - p : strlen (p) + 1);
  180.  
  181.       if (p1 == 0)
  182.     break;
  183.       p = p1 + 1;
  184.  
  185.       /* Dispatch on the char that follows the $.  */
  186.  
  187.       switch (*p)
  188.     {
  189.     case '$':
  190.       /* $$ seen means output one $ to the variable output buffer.  */
  191.       o = variable_buffer_output (o, p, 1);
  192.       break;
  193.  
  194.     case '(':
  195.     case '{':
  196.       /* $(...) or ${...} is the general case of substitution.  */
  197.       {
  198.         char openparen = *p;
  199.         char closeparen = (openparen == '(') ? ')' : '}';
  200.         register char *beg = p + 1;
  201.         int free_beg = 0;
  202.         char *op, *begp;
  203.         char *end, *colon;
  204.  
  205.         op = o;
  206.         begp = p;
  207.         if (handle_function (&op, &begp))
  208.           {
  209.         o = op;
  210.         p = begp;
  211.         break;
  212.           }
  213.  
  214.         /* Is there a variable reference inside the parens or braces?
  215.            If so, expand it before expanding the entire reference.  */
  216.  
  217.         end = index (beg, closeparen);
  218.         if (end == 0)
  219.           {
  220.         /* Unterminated variable reference.  */
  221.         if (reading_filename != 0)
  222.           makefile_fatal (reading_filename, *reading_lineno_ptr,
  223.                   "unterminated variable reference");
  224.         else
  225.           fatal ("unterminated variable reference");
  226.           }
  227.         p1 = lindex (beg, end, '$');
  228.         if (p1 != 0)
  229.           {
  230.         /* BEG now points past the opening paren or brace.
  231.            Count parens or braces until it is matched.  */
  232.         int count = 0;
  233.         for (p = beg; *p != '\0'; ++p)
  234.           {
  235.             if (*p == openparen)
  236.               ++count;
  237.             else if (*p == closeparen && --count < 0)
  238.               break;
  239.           }
  240.         /* If COUNT is >= 0, there were unmatched opening parens
  241.            or braces, so we go to the simple case of a variable name
  242.            such as `$($(a)'.  */
  243.         if (count < 0)
  244.           {
  245.             beg = expand_argument (beg, p); /* Expand the name.  */
  246.             free_beg = 1; /* Remember to free BEG when finished.  */
  247.             end = index (beg, '\0');
  248.           }
  249.           }
  250.         else
  251.           /* Advance P to the end of this reference.  After we are
  252.                  finished expanding this one, P will be incremented to
  253.                  continue the scan.  */
  254.           p = end;
  255.  
  256.         /* This is not a reference to a built-in function and
  257.            any variable references inside are now expanded.
  258.            Is the resultant text a substitution reference?  */
  259.  
  260.         colon = lindex (beg, end, ':');
  261.         if (colon != 0)
  262.           {
  263.         /* This looks like a substitution reference: $(FOO:A=B).  */
  264.         char *subst_beg, *subst_end, *replace_beg, *replace_end;
  265.  
  266.         subst_beg = colon + 1;
  267.         subst_end = index (subst_beg, '=');
  268.         if (subst_end == 0)
  269.           /* There is no = in sight.  Punt on the substitution
  270.              reference and treat this as a variable name containing
  271.              a colon, in the code below.  */
  272.           colon = 0;
  273.         else
  274.           {
  275.             replace_beg = subst_end + 1;
  276.             replace_end = end;
  277.  
  278.             /* Extract the variable name before the colon
  279.                and look up that variable.  */
  280.             v = lookup_variable (beg, colon - beg);
  281.             if (v == 0)
  282.               warn_undefined (beg, colon - beg);
  283.  
  284.             if (v != 0 && *v->value != '\0')
  285.               {
  286.             char *value = (v->recursive ? recursively_expand (v)
  287.                        : v->value);
  288.             char *pattern, *percent;
  289.             if (free_beg)
  290.               {
  291.                 *subst_end = '\0';
  292.                 pattern = subst_beg;
  293.               }
  294.             else
  295.               {
  296.                 pattern = (char *) alloca (subst_end - subst_beg
  297.                                + 1);
  298.                 bcopy (subst_beg, pattern, subst_end - subst_beg);
  299.                 pattern[subst_end - subst_beg] = '\0';
  300.               }
  301.             percent = find_percent (pattern);
  302.             if (percent != 0)
  303.               {
  304.                 char *replace;
  305.                 if (free_beg)
  306.                   {
  307.                 *replace_end = '\0';
  308.                 replace = replace_beg;
  309.                   }
  310.                 else
  311.                   {
  312.                 replace = (char *) alloca (replace_end
  313.                                - replace_beg
  314.                                + 1);
  315.                 bcopy (replace_beg, replace,
  316.                        replace_end - replace_beg);
  317.                 replace[replace_end - replace_beg] = '\0';
  318.                   }
  319.                 
  320.                 o = patsubst_expand (o, value, pattern, replace,
  321.                          percent, (char *) 0);
  322.               }
  323.             else
  324.               o = subst_expand (o, value,
  325.                         pattern, replace_beg,
  326.                         strlen (pattern),
  327.                         end - replace_beg,
  328.                         0, 1);
  329.             if (v->recursive)
  330.               free (value);
  331.               }
  332.           }
  333.           }
  334.  
  335.         if (colon == 0)
  336.           /* This is an ordinary variable reference.
  337.          Look up the value of the variable.  */
  338.         o = reference_variable (o, beg, end - beg);
  339.  
  340.       if (free_beg)
  341.         free (beg);
  342.       }
  343.       break;
  344.  
  345.     case '\0':
  346.       break;
  347.  
  348.     default:
  349.       if (isblank (p[-1]))
  350.         break;
  351.  
  352.       /* A $ followed by a random char is a variable reference:
  353.          $a is equivalent to $(a).  */
  354.       {
  355.         /* We could do the expanding here, but this way
  356.            avoids code repetition at a small performance cost.  */
  357.         char name[5];
  358.         name[0] = '$';
  359.         name[1] = '(';
  360.         name[2] = *p;
  361.         name[3] = ')';
  362.         name[4] = '\0';
  363.         p1 = allocated_variable_expand (name);
  364.         o = variable_buffer_output (o, p1, strlen (p1));
  365.         free (p1);
  366.       }
  367.  
  368.       break;
  369.     }      
  370.  
  371.       if (*p == '\0')
  372.     break;
  373.       else
  374.     ++p;
  375.     }
  376.  
  377.   (void) variable_buffer_output (o, "", 1);
  378.   return initialize_variable_output ();
  379. }
  380.  
  381. /* Expand an argument for an expansion function.
  382.    The text starting at STR and ending at END is variable-expanded
  383.    into a null-terminated string that is returned as the value.
  384.    This is done without clobbering `variable_buffer' or the current
  385.    variable-expansion that is in progress.  */
  386.  
  387. char *
  388. expand_argument (str, end)
  389.      char *str, *end;
  390. {
  391.   char *tmp;
  392.  
  393.   if (*end == '\0')
  394.     tmp = str;
  395.   else
  396.     {
  397.       tmp = (char *) alloca (end - str + 1);
  398.       bcopy (str, tmp, end - str);
  399.       tmp[end - str] = '\0';
  400.     }
  401.  
  402.   return allocated_variable_expand (tmp);
  403. }
  404.  
  405. /* Expand LINE for FILE.  Error messages refer to the file and line where
  406.    FILE's commands were found.  Expansion uses FILE's variable set list.  */
  407.  
  408. char *
  409. variable_expand_for_file (line, file)
  410.      char *line;
  411.      register struct file *file;
  412. {
  413.   char *result;
  414.   struct variable_set_list *save;
  415.  
  416.   if (file == 0)
  417.     return variable_expand (line);
  418.  
  419.   save = current_variable_set_list;
  420.   current_variable_set_list = file->variables;
  421.   reading_filename = file->cmds->filename;
  422.   reading_lineno_ptr = &file->cmds->lineno;
  423.   result = variable_expand (line);
  424.   current_variable_set_list = save;
  425.   reading_filename = 0;
  426.   reading_lineno_ptr = 0;
  427.  
  428.   return result;
  429. }
  430.  
  431. /* Like variable_expand_for_file, but the returned string is malloc'd.
  432.    This function is called a lot.  It wants to be efficient.  */
  433.  
  434. char *
  435. allocated_variable_expand_for_file (line, file)
  436.      char *line;
  437.      struct file *file;
  438. {
  439.   char *value;
  440.  
  441.   char *obuf = variable_buffer;
  442.   unsigned int olen = variable_buffer_length;
  443.  
  444.   variable_buffer = 0;
  445.  
  446.   value = variable_expand_for_file (line, file);
  447.  
  448. #if 0
  449.   /* Waste a little memory and save time.  */
  450.   value = xrealloc (value, strlen (value))
  451. #endif
  452.  
  453.   variable_buffer = obuf;
  454.   variable_buffer_length = olen;
  455.  
  456.   return value;
  457. }
  458.